#ifndef GLTERRAIN_H
#define GLTERRAIN_H


#include "GLObject.h"
#include "../core/common.h"
#include "../core/Color.h"
#include "../core/vector3.h"
#include "../gl/GLTexture.h"
#include "../gl/mygl_ext.h"


const s32 TERRAIN_MAX_SIZE = 500;


class GLTerrain : public GLObject
{
public:
	struct STerrainVertex
	{
		f32 		height;
		Color		color;
		vector3f	normal;
	};


	struct STerrainFace
	{
		vector3f	normal1;
		vector3f 	normal2;
		int 		flags;
	};

public:
	GLTerrain(GLObject* obj) :
		GLObject(obj),
		m_Texture(0),
		m_SizeX(0),
		m_SizeY(0)
	{ }

	void init(s32 sizeX, s32 sizeY);
	void generate(s32 sizeX, s32 sizeY);
	void addHeight(f32 x, f32 y, f32 bsize, f32 dh);
	void addColor(f32 x, f32 y, f32 bsize, f32 dR, f32 dG, f32 dB);

	f32 pickHeight(f32 x, f32 y);

	inline void setTexture(GLTexture* tex) { m_Texture = tex; }

	inline STerrainVertex& getVertex(int ix, int iy) { return m_VertexMap[ix][iy]; }
	inline STerrainFace& getFace(int ix, int iy) { return m_FaceMap[ix][iy]; }

	virtual void onRender();

private:
	void buildNormals();
	void computeVerticeNormal(s32 vx, s32 vy);

private:
	STerrainVertex 	m_VertexMap[TERRAIN_MAX_SIZE + 1][TERRAIN_MAX_SIZE + 1];
	STerrainFace	m_FaceMap[TERRAIN_MAX_SIZE][TERRAIN_MAX_SIZE];

	GLTexture*	m_Texture;
	s32 		m_SizeX;
	s32 		m_SizeY;
};

#endif // GLTERRAIN_H
